package com.hxf.web.action.user;

import java.io.IOException;
import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.hxf.common.DateUtil;
import com.hxf.common.GlobalMPURL;
import com.hxf.dto.AdminRoleDto;
import com.hxf.dto.RoleDto;
import com.hxf.dto.UserDto;
import com.hxf.exception.ServiceException;
import com.hxf.web.action.WebActionBase;
import com.hxf.web.vo.UserRoleVo;

/**
 * 会员管理Action
 * @author tengyicheng
 * @version [1.0, 2013/09/22]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@SuppressWarnings("unchecked")
@Results({@Result(name = "success", location = "../common/success.jsp"), @Result(name = "error", location = "../common/error.jsp"), @Result(name = "msg", location = "../common/msg.jsp")})
public class UserAction extends WebActionBase {
 
	private static final long serialVersionUID = 1L;
	
	private List<UserDto> userList; 
	private List<RoleDto> roleList; 
	private List<UserRoleVo> selectRoleList; 
	
	private String roleIds;
	private String userIds;
	
	private UserDto userDto;
	
	private Integer userId; 				// 用户id
	private String userName; 				// 用户名
	private String realName; 				// 真实姓名
	private int userSex; 					// 性别
	private String userIdNo; 				// 身份证号
	private String userMobile;				// 固定电话
	private String userPhone; 				// 手机
	private String userAddress; 			// 地址
	private String userZipCode; 			// 邮编
	private String userEmail; 				// 邮箱
	private String userPwd; 				// 密码
	private Integer userType; 				// 用户类型
	private Integer status; 				// 状态 1.有效 2.审核中 3.审核不通过 4.锁定 5.无效 
	private String createTime; 				// 创建时间 
	private String userRelation;			// 联系人 
	private Integer restaurantId;			//餐厅编号
	
	/**
	 * 查询用户信息
	 * @return
	 */
	public String userList() {
		try {   
			//菜单权限验证 USER_PRIVILEGE_URL=/user/user!userlist.html
			if (!menuPrivilegeValidate(GlobalMPURL.USER_PRIVILEGE_URL)) {
				return "msg";
			} 
			
			UserDto ud = new UserDto();
			ud.setUserName(userName);
			ud.setUserType(userType);
			ud.setUserIdNo(userIdNo);
			ud.setUserPhone(userPhone);
			UserDto dto = new UserDto();
			//dto = getAdminUser();
			ud.setUserId(getAdminUser().getId());//传当前登录用户 ID 过去
			
			//会员信息
			userList = super.serviceFacade.getUserService().findByUser(ud, targetPage, pageSize);
			//userList = rd.getResultlist();
			this.setRecordCount(userList.size());
		} catch (ServiceException e) {
			logger.debug(e.getMessage(), e);
			e.getMessage();
		}
		return "user-list";
	}
	
	/**
	 * 跳转添加用户信息页面
	 * @return
	 */
	public String userAdd() {
		//菜单权限验证
		if (!menuPrivilegeValidate(GlobalMPURL.USER_PRIVILEGE_URL)) {
			return "msg";
		} 
		
		return "user-add";
	}
	
	/**
	 * 修改用户信息
	 * @return
	 */
	public String userUpdate() { 
		try {
			//菜单权限验证
			if (!menuPrivilegeValidate(GlobalMPURL.USER_PRIVILEGE_URL)) {
				return "msg";
			} 
			userDto = (UserDto) super.serviceFacade.getUserService().get(UserDto.class, userId);
		} catch (ServiceException e) {
			logger.debug(e.getMessage(), e);
		}
		return "user-update";
	}

	/**
	 * 保存或者修改用户信息
	 * @return
	 */
	public String userSaveOrUpdate() {
		try {
			//菜单权限验证 
			if (!menuPrivilegeValidate(GlobalMPURL.USER_PRIVILEGE_URL)) {
				return "msg";
			}
			
			UserDto user = (UserDto)getSession().getAttribute("userDto");
			if (user.getUserId() != null && !"null".equals(user.getUserId())) {  
				//user = (UserDto)super.serviceFacade.getUserService().get(UserDto.class, userId);
			
				//UserDto ud = new UserDto(userId, userName, realName, userSex, userIdNo, userMobile, userPhone, userAddress, userZipCode, userEmail, userPwd, userType, status, DateUtil.getCurrentDate(DateUtil.DATE_PATTERN_M), userRelation);
				//ud.setUserId(userId);
				UserDto ud = new UserDto();
				ud.setUserName(userName);//用户名
				ud.setRealName(realName);//真实姓名
				ud.setUserSex(userSex);//性别
				ud.setUserIdNo(userIdNo);//身份证号码
				ud.setUserMobile(userMobile);//联系电话
				ud.setUserPhone(userPhone);//电话
				ud.setUserAddress(userAddress);//地址
				ud.setUserZipCode(userZipCode);
				ud.setUserEmail(userEmail);//邮箱
				ud.setUserPwd(userPwd);//密码
				ud.setUserType(userType);//类型 1后台管理，2公司人员,3餐厅人员
				ud.setStatus(status);//状态
				ud.setCreateTime(DateUtil.getCurrentDate(DateUtil.DATE_PATTERN_M));//当前时间
				ud.setUserRelation(userRelation);
				ud.setCreateUser(user.getUserName());//创建人
				
				ud.setUserType(user.getUserType());
				//ud.setUserId(user.getUserId());
				ud.setStatus(user.getStatus());
				//ud.setUserName(user.getUserName());
				if(null == user.getParentId() || "null".equals(user.getParentId())){ //为空 是 admin 下级别
					ud.setParentId(user.getUserId());
				}else{
					ud.setParentId(user.getUserId());//存上级的ID
				}
				
				if(null == user.getParentUserId() || "null".equals(user.getParentUserId())){
					ud.setParentUserId(user.getUserId().toString());
				}else{
					ud.setParentUserId(user.getParentUserId()+"#"+user.getUserId());
				}
				
				//ud.setParentUserId();//存上级连起来的Id
				super.serviceFacade.getUserService().save(ud);
			} else {
				//构造方法
				user = new UserDto(userId, userName, realName, userSex, userIdNo, userMobile, userPhone, userAddress, userZipCode, userEmail, userPwd, userType, status, DateUtil.getCurrentDate(DateUtil.DATE_PATTERN_M), userRelation);
				//后台管理员
				user.setUserType(UserDto.USER_TYPE_2);
				user.setStatus(UserDto.USER_STATUS_0);
				super.serviceFacade.getUserService().save(user);
			}
			requestURL = "user!userList.html";
		} catch (ServiceException e) {
			logger.debug(e.getMessage(), e);
		}
		return SUCCESS;
	}
	
	
	
	/**
	 * 删除用户信息
	 * @return
	 */
	public String userDelete() {
		try {
			//菜单权限验证
			if (!menuPrivilegeValidate(GlobalMPURL.USER_PRIVILEGE_URL)) {
				return "msg";
			} 
			super.serviceFacade.getUserService().delete(userDto);
			super.println("success");
		} catch (ServiceException e) { 
			logger.debug(e.getMessage(), e);
		} catch (IOException e) {
			logger.debug(e.getMessage(), e);
		} 
		return null;
	}
	
	/**
	 * 绑定角色页面
	 * @return
	 */
	public String userAddRole() {
		try {
			//菜单权限验证
			if (!menuPrivilegeValidate(GlobalMPURL.USER_PRIVILEGE_URL)) {
				return "msg";
			} 
			userDto = (UserDto)super.serviceFacade.getUserService().get(UserDto.class, userDto.getUserId());
			AdminRoleDto urDto = new AdminRoleDto();
			urDto.setUserId(userDto.getUserId());//点击角色绑定 id
			UserDto user = getUser();//当前登陆用户
			
			selectRoleList = super.serviceFacade.getUserService().findByUserAddRole(urDto,user.getUserId());
			
		} catch (ServiceException e) {
			logger.debug(e.getMessage(), e);
		}
		return "user-add-role";
	}
	
	/**
	 * 保存添加角色
	 * @return
	 */
	public String userAddRoleSave() { 
		try {
			//菜单权限验证
			if (!menuPrivilegeValidate(GlobalMPURL.USER_PRIVILEGE_URL)) {
				return "msg";
			} 
			super.serviceFacade.getUserService().saveUserAddRole(userDto, roleIds);
		} catch (ServiceException e) {
			logger.debug(e.getMessage(), e);
		}
		requestURL = "user!userList.html";
		return SUCCESS;
	}
	
	/**
	 * 更新会员状态
	 * @return
	 */
	public String updateUserStatus() {
		//菜单权限验证
		if (!menuPrivilegeValidate(GlobalMPURL.USER_PRIVILEGE_URL)) {
			return "msg";
		}  
		try {
			if ("".equals(userIds) || null == userIds || status == null) {
				println(-1);
				return null;
			}
			super.serviceFacade.getUserService().updateUserStatus(userIds, status);
			println(0);
		} catch (ServiceException e) {
			logger.debug(e.getMessage(), e);
		} catch (IOException e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}

	public List<UserDto> getUserList() {
		return userList;
	}

	public void setUserList(List<UserDto> userList) {
		this.userList = userList;
	}

	public Integer getUserId() {
		return userId;
	}

	public void setUserId(Integer userId) {
		this.userId = userId;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getRealName() {
		return realName;
	}

	public void setRealName(String realName) {
		this.realName = realName;
	}

	public int getUserSex() {
		return userSex;
	}

	public void setUserSex(int userSex) {
		this.userSex = userSex;
	}

	public String getUserIdNo() {
		return userIdNo;
	}

	public void setUserIdNo(String userIdNo) {
		this.userIdNo = userIdNo;
	}

	public String getUserMobile() {
		return userMobile;
	}

	public void setUserMobile(String userMobile) {
		this.userMobile = userMobile;
	}

	public String getUserPhone() {
		return userPhone;
	}

	public void setUserPhone(String userPhone) {
		this.userPhone = userPhone;
	}

	public String getUserAddress() {
		return userAddress;
	}

	public void setUserAddress(String userAddress) {
		this.userAddress = userAddress;
	}

	public String getUserZipCode() {
		return userZipCode;
	}

	public void setUserZipCode(String userZipCode) {
		this.userZipCode = userZipCode;
	}

	public String getUserEmail() {
		return userEmail;
	}

	public void setUserEmail(String userEmail) {
		this.userEmail = userEmail;
	}

	public String getUserPwd() {
		return userPwd;
	}

	public void setUserPwd(String userPwd) {
		this.userPwd = userPwd;
	}

	public Integer getUserType() {
		return userType;
	}

	public void setUserType(Integer userType) {
		this.userType = userType;
	}

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public String getCreateTime() {
		return createTime;
	}

	public void setCreateTime(String createTime) {
		this.createTime = createTime;
	}

	public List<RoleDto> getRoleList() {
		return roleList;
	}

	public void setRoleList(List<RoleDto> roleList) {
		this.roleList = roleList;
	}

	public List<UserRoleVo> getSelectRoleList() {
		return selectRoleList;
	}

	public void setSelectRoleList(List<UserRoleVo> selectRoleList) {
		this.selectRoleList = selectRoleList;
	}

	public String getRoleIds() {
		return roleIds;
	}

	public void setRoleIds(String roleIds) {
		this.roleIds = roleIds;
	}

	public UserDto getUserDto() {
		return userDto;
	}

	public void setUserDto(UserDto userDto) {
		this.userDto = userDto;
	}

	public String getUserRelation() {
		return userRelation;
	}

	public void setUserRelation(String userRelation) {
		this.userRelation = userRelation;
	}

	public String getUserIds() {
		return userIds;
	}

	public void setUserIds(String userIds) {
		this.userIds = userIds;
	}

	public Integer getRestaurantId() {
		return restaurantId;
	}

	public void setRestaurantId(Integer restaurantId) {
		this.restaurantId = restaurantId;
	}
	
}
